package com.flyox.game.militarychess.client.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import com.flyox.game.militarychess.CONSTS;
import com.flyox.game.militarychess.client.control.MainControl;
import com.flyox.game.militarychess.client.multicast.BroadRecive;
import com.flyox.game.militarychess.client.services.DataService;
import com.flyox.game.militarychess.message.Response;
import com.flyox.game.militarychess.server.Start;

public class LoginJDialog extends javax.swing.JDialog {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static final long serialVersionUID = -115583598939485141L;

	private JLabel jLabelIP;
	private JTextField jTextFieldPort;
	private JTextField jTextFieldName;
	private JButton jButtonCreate;
	private JButton jButtonRefresh;
	private JComboBox jComboBoxIP;
	private JPanel jPanelplayer;
	private JPasswordField jPasswordField;
	private JPanel jPanelAll;
	private JButton jButtonLogin;
	private JLabel jLabelPass;
	private JLabel jLabelName;
	private JLabel jLabelPort;

	private boolean canceled = false;
	MainControl control = null;

	public LoginJDialog(MainControl control, JFrame frame) {
		super(frame, true);
		this.control = control;
		String message = "127.0.0.1";
		initGUI(message);
	}

	private void initGUI(String message) {
		try {
			{
				getContentPane().setLayout(null);
				this.setTitle("\u767b\u5f55");
				addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						canceled = true;
					}
				});
				{
					jButtonLogin = new JButton();
					getContentPane().add(jButtonLogin);
					jButtonLogin.setText("\u767b\u5f55");
					jButtonLogin.setBounds(253, 124, 84, 22);
					jButtonLogin.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonLoginActionPerformed(evt);
						}
					});
				}
				{
					jPanelAll = new JPanel();
					getContentPane().add(jPanelAll);
					jPanelAll.setBounds(12, 13, 325, 36);
					jPanelAll.setBorder(new LineBorder(new java.awt.Color(0, 0, 0), 1, false));
					jPanelAll.setLayout(null);
					{
						jLabelIP = new JLabel();
						jPanelAll.add(jLabelIP);
						jLabelIP.setText("IP\uff1a");
						jLabelIP.setBounds(24, 10, 24, 15);
					}
					{
						jTextFieldPort = new JTextField();
						jPanelAll.add(jTextFieldPort);
						jTextFieldPort.setBounds(226, 6, 80, 22);
						jTextFieldPort.setText("8808");
						jTextFieldPort.setEditable(false);
					}
					{
						jLabelPort = new JLabel();
						jPanelAll.add(jLabelPort);
						jLabelPort.setText("\u7aef\u53e3\uff1a");
						jLabelPort.setBounds(190, 10, 36, 15);
					}
					{
						ComboBoxModel jComboBoxIPModel = new DefaultComboBoxModel(new String[] { message });

						jComboBoxIP = new JComboBox();
						jPanelAll.add(jComboBoxIP);
						jComboBoxIP.setModel(jComboBoxIPModel);
						jComboBoxIP.setBounds(54, 6, 102, 22);
						jComboBoxIP.setSize(100, 22);
						jComboBoxIP.setEditable(true);
						jComboBoxIP.setFocusable(true);
					}
				}
				{
					jPanelplayer = new JPanel();
					getContentPane().add(jPanelplayer);
					jPanelplayer.setBounds(12, 70, 325, 36);
					jPanelplayer.setBorder(new LineBorder(new java.awt.Color(0, 0, 0), 1, false));
					jPanelplayer.setLayout(null);
					{
						jLabelName = new JLabel();
						jPanelplayer.add(jLabelName);
						jLabelName.setText("\u767b\u5f55\u540d\uff1a");
						jLabelName.setBounds(6, 6, 51, 22);
					}
					{
						jTextFieldName = new JTextField();
						jPanelplayer.add(jTextFieldName);
						jTextFieldName.setBounds(57, 6, 100, 22);
						Random r = new Random();
						jTextFieldName.setText("crazy" + r.nextInt(100));
					}
					{
						jLabelPass = new JLabel();
						jPanelplayer.add(jLabelPass);
						jLabelPass.setText("\u5bc6\u7801\uff1a");
						jLabelPass.setBounds(187, 10, 36, 15);
					}
					{
						jPasswordField = new JPasswordField();
						jPanelplayer.add(jPasswordField);
						jPasswordField.setBounds(223, 6, 80, 22);
						jPasswordField.setText("crazy");
					}
				}
				{
					jButtonRefresh = new JButton();
					getContentPane().add(jButtonRefresh);
					jButtonRefresh.setText("\u5237\u65b0");
					jButtonRefresh.setBounds(132, 124, 84, 22);
					jButtonRefresh.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonRefreshActionPerformed(evt);
						}
					});
				}
				{
					jButtonCreate = new JButton();
					getContentPane().add(jButtonCreate);
					jButtonCreate.setText("\u521b\u5efa\u4e3b\u673a");
					jButtonCreate.setBounds(12, 124, 84, 22);
					jButtonCreate.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonCreateActionPerformed(evt);
						}
					});
				}
			}
			this.setSize(360, 200);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void jButtonLoginActionPerformed(ActionEvent evt) {
		// check input
		jComboBoxIP.getSelectedItem().toString();
		if (jComboBoxIP.getSelectedItem().toString().length() < 1) {
			JOptionPane.showMessageDialog(this, "请正确的输入ip");
			jComboBoxIP.setFocusable(true);
			return;
		}
		if (jTextFieldPort.getText().length() < 1) {
			JOptionPane.showMessageDialog(this, "请正确的输入端口号");
			jTextFieldPort.setFocusable(true);
			return;
		}
		if (jTextFieldName.getText().length() < 1) {
			JOptionPane.showMessageDialog(this, "请正确的输入名称");
			jTextFieldName.setFocusable(true);
			return;
		}
		if (jPasswordField.getPassword().length < 1) {
			JOptionPane.showMessageDialog(this, "请正确的输入密码");
			jPasswordField.setFocusable(true);
			return;
		}
		//
		control.setLoginView(LoginJDialog.this);
		control.login(jComboBoxIP.getSelectedItem().toString(), Integer.parseInt(jTextFieldPort.getText()));

	}

	public void showLoginRes(Response response) {
		if (response == null || response.getRes() == CONSTS.RESPONSE_ERROR) {
			JOptionPane.showMessageDialog(this, jTextFieldName.getText() + " 已登录过！");
			control.closeSession();
			return;
		}
		//
		DataService dataService = new DataService();
		dataService.getPlayer().setName(jTextFieldName.getText());
		dataService.getPlayer().setPass(new String(jPasswordField.getPassword()));
		dataService.getPlayer().setStates(CONSTS.PLAYER_STATS_LOGIN);
		dataService.setServerIP(jComboBoxIP.getSelectedItem().toString());
		dataService.setServerPort(Integer.parseInt(jTextFieldPort.getText()));
		dataService.setDesks(response.getDesks());

		this.dispose();
	}

	public boolean isCanceled() {
		return canceled;
	}

	public void setCanceled(boolean canceled) {
		this.canceled = canceled;
	}

	public JTextField getJTextFieldName() {
		return jTextFieldName;
	}

	public JPasswordField getJPasswordField() {
		return jPasswordField;
	}

	private void jButtonCreateActionPerformed(ActionEvent evt) {
		Start.main(null);
	}

	private void jButtonRefreshActionPerformed(ActionEvent evt) {
		BroadRecive br = BroadRecive.getInstance();
		String message = br.getServerMsg();
		System.out.println(message);
		ComboBoxModel jComboBoxIPModel = new DefaultComboBoxModel(new String[] { message });
		jComboBoxIP.setModel(jComboBoxIPModel);
		// jComboBoxIP.addItem(message);
	}

}
